package com.shanling.shanlingcontroller.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.chipsguide.lib.bluetooth.entities.BluetoothDeviceMusicSongEntity;
import com.chipsguide.lib.bluetooth.managers.BluetoothDeviceManager;
import com.chipsguide.lib.bluetooth.managers.BluetoothDeviceMusicManager;
import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.manager.DeviceMusicManager;
import com.shanling.shanlingcontroller.utils.FormatUtil;
import com.shanling.shanlingcontroller.utils.UIUtils;
import com.shanling.shanlingcontroller.view.DeviceMusicListPopup;
import com.shanling.shanlingcontroller.view.MusicListPopup;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xiaobai on 2019/12/12.
 */

public class DeviceMusicPlayActivity extends BaseAppCompatActivity implements DeviceMusicManager.OnDeviceMusicSongChangedListener {

    private static final String CURRENT_ENTITY = "CURRENT_ENTITY";

    @BindView(R.id.tv_song_name)
    TextView mTvSongName;
    @BindView(R.id.tv_singer)
    TextView mTvSinger;
    @BindView(R.id.tv_startTime)
    TextView mTvStartTime;
    @BindView(R.id.musicTimeSb)
    SeekBar mMusicTimeSb;
    @BindView(R.id.tv_endTime)
    TextView mTvEndTime;
    @BindView(R.id.iv_playmode)
    ImageView mIvPlayMode;
    @BindView(R.id.iv_play)
    ImageView mIvPlay;
    @BindView(R.id.iv_list)
    ImageView mIvList;

    private DeviceMusicManager mDeviceMusicManager;
    private BluetoothDeviceMusicSongEntity mCurrentEntity;
    private DeviceMusicListPopup mMusicListPopup;

    public static void launch(Context context, BluetoothDeviceMusicSongEntity mCurrentEntity){
        Intent intent = new Intent(context, DeviceMusicPlayActivity.class);
        intent.putExtra(CURRENT_ENTITY, mCurrentEntity);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CustomApplication.getInstance().getBluetoothDeviceManager().setMode(BluetoothDeviceManager.Mode.CARD);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        mCurrentEntity = (BluetoothDeviceMusicSongEntity) extras.getSerializable(CURRENT_ENTITY);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_device_music_play;
    }


    @Override
    protected void initViewsAndEvents() {
        mMusicListPopup = new DeviceMusicListPopup(this);
        mDeviceMusicManager = DeviceMusicManager.getInstance();
        mDeviceMusicManager.getProgressUpdate();
        mDeviceMusicManager.addOnDeviceMusicSongChangedListener(this);
    }


    @Override
    protected void initData() {
        mTvSongName.setText(mCurrentEntity.getName());
        mTvSinger.setText(mCurrentEntity.getArtist() == null || TextUtils.isEmpty(mCurrentEntity.getArtist()) ? getString(R.string.unknown) : mCurrentEntity.getArtist());
        updatePlayView(mDeviceMusicManager.getCurrentPlayState());
        updatePlayMode(mDeviceMusicManager.getCurrentLoopMode());
    }

    private void updatePlayView(int playState){
        switch (playState){
            case BluetoothDeviceMusicManager.PlayState.PAUSED:
                mIvPlay.setImageResource(R.drawable.player_btn_play);
                break;
            case BluetoothDeviceMusicManager.PlayState.PLAYING:
                mIvPlay.setImageResource(R.drawable.player_btn_pause);
                break;
        }
    }

    private void updatePlayMode(int mode){
        switch (mode){
            case BluetoothDeviceMusicManager.LoopMode.SHUFFLE:
                mIvPlayMode.setImageResource(R.drawable.player_icon_shuffle);
                break;
            case BluetoothDeviceMusicManager.LoopMode.SINGLE:
                mIvPlayMode.setImageResource(R.drawable.player_icon_single);
                break;
            case BluetoothDeviceMusicManager.LoopMode.ALL:
                mIvPlayMode.setImageResource(R.drawable.player_icon_list_circulation);
                break;
        }
    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }
    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }
    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @OnClick({R.id.iv_back, R.id.iv_playmode, R.id.iv_last, R.id.iv_play, R.id.lv_next, R.id.iv_list})
    public void onViewClicked(View view) {
        if (UIUtils.INSTANCE.isFastClick()) return;
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_playmode:
                if (mDeviceMusicManager.getCurrentLoopMode() == BluetoothDeviceMusicManager.LoopMode.ALL){
                    mDeviceMusicManager.setLoopMode(BluetoothDeviceMusicManager.LoopMode.SHUFFLE);
                }else if (mDeviceMusicManager.getCurrentLoopMode() == BluetoothDeviceMusicManager.LoopMode.SHUFFLE){
                    mDeviceMusicManager.setLoopMode(BluetoothDeviceMusicManager.LoopMode.SINGLE);
                }else if (mDeviceMusicManager.getCurrentLoopMode() == BluetoothDeviceMusicManager.LoopMode.SINGLE){
                    mDeviceMusicManager.setLoopMode(BluetoothDeviceMusicManager.LoopMode.ALL);
                }else {
                    mDeviceMusicManager.setLoopMode(BluetoothDeviceMusicManager.LoopMode.ALL);
                }
                break;
            case R.id.iv_last:
                mDeviceMusicManager.previous();
                break;
            case R.id.iv_play:
                if (mDeviceMusicManager.getCurrentPlayState() == BluetoothDeviceMusicManager.PlayState.PAUSED){
                    mDeviceMusicManager.play();
                }else {
                    mDeviceMusicManager.pause();
                }
                break;
            case R.id.lv_next:
                mDeviceMusicManager.next();
                break;
            case R.id.iv_list:
                mMusicListPopup.show(DeviceMusicManager.getInstance().getDeviceMusicSongEntities(), mIvList);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDeviceMusicManager.removeOnDeviceMusicSongChangedListener(this);
    }

    @Override
    public void onDeviceMusicSongListChanged() {

    }

    @Override
    public void onDeviceMusicLoopModeChanged(int mode) {
        updatePlayMode(mode);
    }

    @Override
    public void onDeviceMusicPlayStateChanged(int playState) {
        updatePlayView(playState);
    }

    @Override
    public void onDeviceMusicSongChanged(BluetoothDeviceMusicSongEntity bluetoothDeviceMusicSongEntity) {
        mTvSongName.setText(bluetoothDeviceMusicSongEntity.getName());
        mTvSinger.setText(bluetoothDeviceMusicSongEntity.getArtist() == null ? getString(R.string.unknown) : bluetoothDeviceMusicSongEntity.getArtist());
    }

    @Override
    public void onDeviceMusicProgressUpdate(int currentPosition, int duration) {
        Log.e("MISS", " onDeviceMusicProgressUpdate ---- " + currentPosition + " --- " + duration);
        mMusicTimeSb.setMax(duration);
        mMusicTimeSb.setProgress(currentPosition);
        mTvStartTime.setText(FormatUtil.INSTANCE.formatTime(currentPosition));
        mTvEndTime.setText(FormatUtil.INSTANCE.formatTime(duration));
    }
}
